#define _CRT_SECURE_NO_WARNINGS

#include"Queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->head;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}
void QueuePush(Queue* pq, QDatatype x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newnode == NULL)
	{
		perror("malloc fail!!");
		exit(1);
	}
	else
	{
		newnode->next = NULL;
		newnode->data = x;
	}
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	QueueNode* del = pq->head;
	pq->head = del->next;
	free(del);
	del = NULL;
	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}
}
QDatatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	return pq->head->data;
}
QDatatype Queueback(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	return pq->tail->data;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	int cnt = 0;
	QueueNode* cur = pq->head;
	while (cur)
	{
		cnt++;
		cur = cur->next;
	}
	return cnt;
}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}
